package com.yiren.dbaa.module.device.view.activity;

import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.widget.GridView;
import android.widget.TextView;

import com.yiren.backstage.currency.bean.CarStateBean;
import com.yiren.dbaa.R;
import com.yiren.dbaa.activity.BaseActivity;
import com.yiren.dbaa.view.adapter.GearCorrectAdapter;
import com.yiren.dbaa.view.toast.MessageBox;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * @author lwb
 * @date :2022/1/19 14:59
 * @description: 档位学习、传动比
 */
public class GearCorrectActivity extends BaseActivity {

    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.tv_right)
    TextView tvRight;
    @BindView(R.id.tv_speed_dog)
    TextView tvSpeedDog;   //速度
    @BindView(R.id.tv_rotate_dog)
    TextView tvRotateDog;  //转速
    @BindView(R.id.tv_gear_dog)
    TextView tvGearDog;    //档位
    @BindView(R.id.gv_gear_correct)
    GridView gvGearCorrect;

    private GearCorrectAdapter gearCorrectAdapter;//档位矫正适配器
    public static CarStateBean carState = CarStateBean.getInstance();//车辆状态
    private Runnable stateInfoRunnable;//状态信息线程类
    private Handler handler;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_gear_correct);
        ButterKnife.bind(this);
        tvTitle.setText("传动比学习");
        tvRight.setVisibility(View.VISIBLE);
        tvRight.setText("保存");
        gearCorrectAdapter = new GearCorrectAdapter(GearCorrectActivity.this);
        gvGearCorrect.setAdapter(gearCorrectAdapter);
        handler = new Handler();
        refresh();
    }


    @OnClick({R.id.iv_back, R.id.tv_right})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.iv_back:
                finish();
                break;
            case R.id.tv_right:  //保存
                gearCorrectAdapter.save();
                MessageBox.show(this, "保存成功");
                break;
        }
    }


    private void refresh() {
        stateInfoRunnable = new Runnable() {
            @Override
            public void run() {
                tvSpeedDog.setText("车速:" + (int) carState.getSpeed() + "码");
                tvRotateDog.setText("转速:" + (int) carState.getRotateSpeed() + "转");
                if (null != carState.getGear()) {
                    tvGearDog.setText("档位：" + carState.getGear().getName());
                }
                handler.postDelayed(this, 100);
            }
        };
        stateInfoRunnable.run();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        handler.removeCallbacks(stateInfoRunnable);
    }
}
